/**
 * Class TuiContentHandler
 * Used to parse the TUI input XML file
 *
 * Class Element represents objects built while parsing
 * 
 * @created Mar. 2, 2007
 * @author abernier
 * @author adelaye
 * @author jprieur
 */
package tui;

import java.lang.Object.*;
import java.lang.reflect.*;

import org.xml.sax.*;

import org.xml.sax.helpers.*;

import exception.*;


import objects.*;
import comRadiusController.*;

import objects.distribution.*;

import sleeping.*;

import utils.Debug;
import kernel.*;
import java.util.*;

import communication.*;
import communication.secondLayer.*;
import communication.thirdLayer.*;
import results.*;


public class TuiContentHandler extends DefaultHandler
{
    private Stack<Element> _elements;
    private Simulator _kernel;
    public String _outputDir = new String("");

    private Vector<DataType> _objects;
    
    // attributes to store the object being built
    private DataMobileStation _mobileStation;
    private DataSink _sink;
    private DataSimulator _dataSimulator;
    private DataSensor _sensor;
    private DataSensorNetwork _sensorNetwork;
    private DataSensorSimulator _sensorSimulator;
    private DataBaseStation _baseStation;
    private DataObstacle _obstacle;
    private Obstacle _objObst;
    private MobileStation _objMobileStation;
    private BaseStation _objBaseStation;
    private SensorNetwork _objSensorNetwork;
    private Sensor _objSensor;
    private Sink _objSink;
    private DataTrajectoryMove _move;
    private DataTrajectoryPause _pause;
    private Vector _traj;
    private DataWishedEmission _emission;
    private DataWishedEmission3 _emission3;
    private Shape _shape;

    private String _prevqName;
    private String _currentTypeStation;
    
    private String _currentTypeTrajectory;
    private String _typeGeneralProbe;
    private String _typeMobileProbe;
    private String _typeBaseProbe;
    private String _typeSinkProbe;
    private String _typeSensorNetworkProbe;
    private String _typeEmission;
    private String _probeType;
    int _mode;
    private boolean _inObstacle = false;
    private boolean _inSensorNetwork = false;
    
    private SecondLayerProtocol _slp;
    private ThirdLayerProtocol _tlp;
    private CommunicationManager _myCommunicationM;


    public TuiContentHandler(Simulator kernel)
    {
	_kernel = kernel;
	_elements = new Stack<Element>();
	_objects = new Vector<DataType>();
	_mode=0;
    }

    public TuiContentHandler(SensorSimulator kernel)
    {
	_kernel = kernel;
	_mode=3;
	_elements = new Stack<Element>();
	_objects = new Vector<DataType>();
    }
    
    public TuiContentHandler(Simulator kernel, String outDir)
    {
	_kernel = kernel;
	_elements = new Stack<Element>();
	_objects = new Vector<DataType>();
	_mode=0;
	_outputDir=new String(outDir);
    }

    public TuiContentHandler(SensorSimulator kernel, String outDir)
    {
	_kernel = kernel;
	_mode=3;
	_elements = new Stack<Element>();
	_objects = new Vector<DataType>();
	_outputDir=new String(outDir);
    }

    public void startDocument() throws SAXException {
	//	Debug.getConfigLogger().info("start document \n"); 
    }

    public void startElement(String uri, 
			     String localName, 
			     String qName, 
			     Attributes att)
	throws SAXException
    {
	_elements.push(new Element(qName,att));

	if(qName.equals("mobile")){
	    _currentTypeStation = "mobile";
	    _probeType=new String("mobile");
	    _traj=new Vector();
	    _mobileStation = new DataMobileStation();
	    _objects.add(_mobileStation);

	    //parameters
	    _mobileStation.setName(att.getValue("name"));
	    _mobileStation.setCoordX(Double.parseDouble(att.getValue("x")));
	    _mobileStation.setCoordY(Double.parseDouble(att.getValue("y")));
	    if(att.getValue("maxReachRadius") != null) _mobileStation.setMaxReachRadius(Double.parseDouble(att.getValue("maxReachRadius")));
	    if(att.getValue("carrierRate") != null) _mobileStation.setCarrierRate(Double.parseDouble(att.getValue("carrierRate")));
	    if(att.getValue("radiusController") != null)
	    {
	        String s=att.getValue("radiusController");
	        if(s.equals("MaxReachController")) _mobileStation.setController(new MaxReachController());
	        if(s.equals("ThreeNeighborsController")) _mobileStation.setController(new ThreeNeighborsController());
	        if(s.equals("VoronoiController")) _mobileStation.setController(new VoronoiController());
            }
	    if(att.getValue("initialBatteryLevel") != null) _mobileStation.setBatteryLevel(Double.parseDouble(att.getValue("initialBatteryLevel")));
	    // Consumption parameters
	    double[] cons;
	    cons=new double[4];
	    if(att.getValue("waitingStateConsumption") != null) cons[0]=Double.parseDouble(att.getValue("waitingStateConsumption"));
	    if(att.getValue("listeningStateConsumption") != null) cons[1]=Double.parseDouble(att.getValue("listeningStateConsumption"));	    
	    if(att.getValue("speakingStateConsumption") != null) cons[2]=Double.parseDouble(att.getValue("speakingStateConsumption"));
	    if(att.getValue("sleepingStateConsumption") != null) cons[3]=Double.parseDouble(att.getValue("sleepingStateConsumption"));	    
	    _mobileStation.setConsumption(cons);

	    // Birth and death
	    if(att.getValue("birth") != null) _mobileStation.setBirth(Integer.parseInt(att.getValue("birth")));
	    if(att.getValue("death") != null) _mobileStation.setDeath(Integer.parseInt(att.getValue("death")));
	    
            // Frequencies
            if(att.getValue("minFreq") != null) _mobileStation.setFmin(Float.parseFloat(att.getValue("minFreq")));
            if(att.getValue("maxFreq") != null) _mobileStation.setFmax(Float.parseFloat(att.getValue("maxFreq")));
            if(att.getValue("nbFreq") != null) _mobileStation.setNbFreq(Integer.parseInt(att.getValue("nbFreq")));

            // Intelligence
            if(att.getValue("intelligence") != null) _mobileStation.setAiLevel(Integer.parseInt(att.getValue("intelligence")));
	    _objMobileStation=new MobileStation(_kernel.getSimulator(),_mobileStation.getCoordX(),_mobileStation.getCoordY());           
	}
	if(qName.equals("sink")){
	    _probeType=new String("sink");
	    _currentTypeStation = "sink";
	    _sink = new DataSink();
	    _objects.add(_sink);

	    //parameters
	    _sink.setName(att.getValue("name"));
	    _sink.setCoordX(Double.parseDouble(att.getValue("x")));
	    _sink.setCoordY(Double.parseDouble(att.getValue("y")));
	    if(att.getValue("maxReachRadius") != null) _sink.setMaxReachRadius(Double.parseDouble(att.getValue("maxReachRadius")));
	    if(att.getValue("carrierRate") != null) _sink.setCarrierRate(Double.parseDouble(att.getValue("carrierRate")));
	    if(att.getValue("radiusController") != null) _sink.setController(att.getValue("radiusController"));
	    
	    if(att.getValue("initialBatteryLevel") != null) _sink.setBatteryLevel(Double.parseDouble(att.getValue("initialBatteryLevel")));
	    // Consumption parameters
	    double[] cons;
	    cons=new double[4];
	    if(att.getValue("waitingStateConsumption") != null) cons[0]=Double.parseDouble(att.getValue("waitingStateConsumption"));
	    if(att.getValue("listeningStateConsumption") != null) cons[1]=Double.parseDouble(att.getValue("listeningStateConsumption"));	    
	    if(att.getValue("speakingStateConsumption") != null) cons[2]=Double.parseDouble(att.getValue("speakingStateConsumption"));
	    if(att.getValue("sleepingStateConsumption") != null) cons[3]=Double.parseDouble(att.getValue("sleepingStateConsumption"));	    
	    _sink.setConsumption(cons);

	    // Birth and death
	    if(att.getValue("birth") != null) _sink.setBirth(Integer.parseInt(att.getValue("birth")));
	    if(att.getValue("death") != null) _sink.setDeath(Integer.parseInt(att.getValue("death")));
	    
            // Frequencies
            if(att.getValue("minFreq") != null) _sink.setFmin(Float.parseFloat(att.getValue("minFreq")));
            if(att.getValue("maxFreq") != null) _sink.setFmax(Float.parseFloat(att.getValue("maxFreq")));
            if(att.getValue("nbFreq") != null) _sink.setNbFreq(Integer.parseInt(att.getValue("nbFreq")));

	    if(att.getValue("radiusController") != null)
	    {
	        String s=att.getValue("radiusController");
	        if(s.equals("MaxReachController")) _sink.setController(new MaxReachController());
	        if(s.equals("ThreeNeighborsController")) _sink.setController(new ThreeNeighborsController());
	        if(s.equals("VoronoiController")) _sink.setController(new VoronoiController());
            }

            // Intelligence
            if(att.getValue("intelligence") != null) _sink.setAiLevel(Integer.parseInt(att.getValue("intelligence")));
  	    _objSink=new Sink(((SensorSimulator)(_kernel.getSimulator())),_sink.getCoordX(),_sink.getCoordY());

	}

	if(qName.equals("sensor")){
	    _currentTypeStation = "sensor";
	    _sensor = new DataSensor();
	    _objects.add(_sensor);

	    //parameters
	    _sensor.setName(att.getValue("name"));
	    _sensor.setCoordX(Double.parseDouble(att.getValue("x")));
	    _sensor.setCoordY(Double.parseDouble(att.getValue("y")));
	    if(att.getValue("maxReachRadius") != null) _sensor.setMaxReachRadius(Double.parseDouble(att.getValue("maxReachRadius")));
	    if(att.getValue("carrierRate") != null) _sensor.setCarrierRate(Double.parseDouble(att.getValue("carrierRate")));
	    if(att.getValue("radiusController") != null) _sensor.setController(att.getValue("radiusController"));
	    
	    if(att.getValue("initialBatteryLevel") != null) _sensor.setBatteryLevel(Double.parseDouble(att.getValue("initialBatteryLevel")));
	    // Consumption parameters
	    double[] cons;
	    cons=new double[4];
	    if(att.getValue("waitingStateConsumption") != null) cons[0]=Double.parseDouble(att.getValue("waitingStateConsumption"));
	    if(att.getValue("listeningStateConsumption") != null) cons[1]=Double.parseDouble(att.getValue("listeningStateConsumption"));	    
	    if(att.getValue("speakingStateConsumption") != null) cons[2]=Double.parseDouble(att.getValue("speakingStateConsumption"));
	    if(att.getValue("sleepingStateConsumption") != null) cons[3]=Double.parseDouble(att.getValue("sleepingStateConsumption"));
	    _sensor.setConsumption(cons);

	    // Birth and death
	    if(att.getValue("birth") != null) _sensor.setBirth(Integer.parseInt(att.getValue("birth")));
	    if(att.getValue("death") != null) _sensor.setDeath(Integer.parseInt(att.getValue("death")));
	    
    	    if(att.getValue("radiusController") != null)
	    {
	        String s=att.getValue("radiusController");
	        if(s.equals("MaxReachController")) _sensor.setController(new MaxReachController());
	        if(s.equals("ThreeNeighborsController")) _sensor.setController(new ThreeNeighborsController());
	        if(s.equals("VoronoiController")) _sensor.setController(new VoronoiController());
            }
	    
            // Frequencies
            if(att.getValue("minFreq") != null) _sensor.setFmin(Float.parseFloat(att.getValue("minFreq")));
            if(att.getValue("maxFreq") != null) _sensor.setFmax(Float.parseFloat(att.getValue("maxFreq")));
            if(att.getValue("nbFreq") != null) _sensor.setNbFreq(Integer.parseInt(att.getValue("nbFreq")));

            // Intelligence
            if(att.getValue("intelligence") != null) _sensor.setAiLevel(Integer.parseInt(att.getValue("intelligence")));           
            if(att.getValue("measureRadius") != null) _sensor.setMeasurementRadius(Double.parseDouble(att.getValue("measureRadius")));
            _objSensor=new Sensor(_objSensorNetwork,_kernel.getSimulator(),_sensor.getCoordX(),_sensor.getCoordY());
	}
	if(qName.equals("sensornetwork")){
            _inSensorNetwork=true;
            _probeType=new String("sensornetwork");
	    _sensorNetwork = new DataSensorNetwork();
	    _objects.add(_sensorNetwork);

	    //parameters
	    _sensorNetwork.setName(att.getValue("name"));
	    _sensorNetwork.setAmount(Integer.parseInt(att.getValue("amount")));
	    if(att.getValue("distribution") != null)
	    {
	      if(att.getValue("distribution").equals("gridDistribAlgo")) _sensorNetwork.setDistributionAlgo(new GridDistribAlgo());
	      if(att.getValue("distribution").equals("RandomDistribAlgo")) _sensorNetwork.setDistributionAlgo(new RandomDistribAlgo());
	      if(att.getValue("distribution").equals("snailDistribAlgo")) { _sensorNetwork.setDistributionAlgo(new SnailDistribAlgo()); }
	    }

	    if(att.getValue("sleepingMode") != null)
	    {
	      if(att.getValue("sleepingMode").equals("DrowsinessAlgo")) _sensorNetwork.setSleepingAlgo(new DrowsinessAlgo());
	      if(att.getValue("sleepingMode").equals("NoSleeplingAlgo")) _sensorNetwork.setSleepingAlgo(new NoSleepingAlgo());
	      if(att.getValue("sleepingMode").equals("RACPAlgo")) _sensorNetwork.setSleepingAlgo(new RACPAlgo());
	      if(att.getValue("sleepingMode").equals("RandomAlgo")) _sensorNetwork.setSleepingAlgo(new RandomAlgo());
	    }
	    
	    if(att.getValue("kCover") != null) _sensorNetwork.setKCover(Integer.parseInt(att.getValue("kCover")));
	    if(att.getValue("sleepingPeriod") != null) _sensorNetwork.setSleepingPeriod(Integer.parseInt(att.getValue("sleepingPeriod")));
	    _objSensorNetwork = new SensorNetwork();

        }

	if(qName.equals("base")){
	    _probeType=new String("base");
	    _currentTypeStation = "base";
	    _baseStation = new DataBaseStation();
	    _objects.add(_baseStation);

	    //parameters
	    _baseStation.setName(att.getValue("name"));
	    _baseStation.setCoordX(Double.parseDouble(att.getValue("x")));
	    _baseStation.setCoordY(Double.parseDouble(att.getValue("y")));
	    if(att.getValue("maxReachRadius") != null) _baseStation.setMaxReachRadius(Double.parseDouble(att.getValue("maxReachRadius")));
	    if(att.getValue("carrierRate") != null) _baseStation.setCarrierRate(Double.parseDouble(att.getValue("carrierRate")));
            if(att.getValue("minFreq") != null) _baseStation.setFmin(Float.parseFloat(att.getValue("minFreq")));
            if(att.getValue("maxFreq") != null) _baseStation.setFmax(Float.parseFloat(att.getValue("maxFreq")));
            _objBaseStation=new BaseStation(_baseStation.getCoordX(),_baseStation.getCoordY(),_kernel.getSimulator());
	}

	if(qName.equals("communication")){
	    if(att.getValue("layer")!=null)
	    {
	    if((Integer.parseInt(att.getValue("layer")))==2)
	    {
	        _typeEmission = new String("wished");
   	        _emission = new DataWishedEmission();
	        _emission.setStartTime(Double.parseDouble(att.getValue("start")));
	        Vector parameters = new Vector();
	        parameters.add(att.getValue("params"));
	        _emission.setParameters(parameters);
  	        // adds the communication to the related station
	        if(_currentTypeStation.equals("base")){
		    Vector emissions = _baseStation.getWishedEmission();
		    emissions.add(_emission);
		    _baseStation.setWishedEmission(emissions);
	        }
	        if(_currentTypeStation.equals("mobile")){
		    Vector emissions = _mobileStation.getWishedEmission();
		    emissions.add(_emission);
		    _mobileStation.setWishedEmission(emissions);
	        }
	        if(_currentTypeStation.equals("sink")){
		    Vector emissions = _sink.getWishedEmission();
		    emissions.add(_emission);
		    _sink.setWishedEmission(emissions);
	        }
	        if(_currentTypeStation.equals("sensor")){
		    Vector emissions = _sensor.getWishedEmission();
		    emissions.add(_emission);
		    _sensor.setWishedEmission(emissions);
	        }	        	        
	    }
	    if(Integer.parseInt(att.getValue("layer"))==3)
	    {
	        _typeEmission = new String("wished3");
   	        _emission3 = new DataWishedEmission3();
	        _emission3.setStartTime(Integer.parseInt(att.getValue("start")));
	        Vector parameters = new Vector();
	        parameters.add(att.getValue("bytes"));
	        _emission3.setParameters(parameters);
  	        // adds the communication to the related station
	        if(_currentTypeStation.equals("mobile")){
		    Vector emissions = _mobileStation.getWishedEmission3();
		    emissions.add(_emission3);
		    _mobileStation.setWishedEmission3(emissions);
	        }
	        if(_currentTypeStation.equals("sink")){
		    Vector emissions = _sink.getWishedEmission3();
		    emissions.add(_emission3);
		    _sink.setWishedEmission3(emissions);
	        }	        
	        if(_currentTypeStation.equals("sensor")){
		    Vector emissions = _sensor.getWishedEmission3();
		    emissions.add(_emission3);
		    _sensor.setWishedEmission3(emissions);
	        }	        
	    }
	    }
	}
	if(qName.equals("destination")){
            if(_typeEmission.equals("wished"))
            {
  	        Vector dest = _emission.getReceivers();
	        dest.add(att.getValue("name"));
	        _emission.setReceivers(dest);
           }
            if(_typeEmission.equals("wished3"))
            {
  	        Vector dest = _emission3.getReceivers();
	        dest.add(att.getValue("name"));
	        _emission3.setReceivers(dest);
           }
	}

        // Trajectories
        if(qName.equals("trajectory")) {
            if(_currentTypeStation.equals("mobile")) {
                if(att.getValue("repeat").equals("true")) _mobileStation.setRepeat(true);
            }
            if(_currentTypeStation.equals("sink")) {
//                if(att.getValue("repeat").equals("true")) _sink.setRepeat(true);
            }
        }
	if(qName.equals("pause")){
	    _pause = new DataTrajectoryPause(0);
            if(att.getValue("pause") != null) _pause.setDuration(Double.parseDouble(att.getValue("duration")));
            _traj.add(_pause);
	}
	if(qName.equals("move")){
	    _move=new DataTrajectoryMove();
	    {
	      String s=att.getValue("type");
	      if(s.equals("QuarterCircleHorair")) _move.setType(_move.TYPEQUARTERCIRCLEHORA);
	      if(s.equals("QuarterCircleTrigo")) _move.setType(_move.TYPEQUARTERCIRCLETRIGO);
	      if(s.equals("SemiCircleHorair")) _move.setType(_move.TYPESEMICIRCLEHORA);
	      if(s.equals("SemiCircleTrigo")) _move.setType(_move.TYPESEMICIRCLETRIGO);
	      if(s.equals("Line")) _move.setType(_move.TYPELINE);
	      if(s.equals("Random")) _move.setType(_move.TYPERANDOM);
            }
            if(att.getValue("xdest") != null) _move.setXdest(Double.parseDouble(att.getValue("xdest")));
            if(att.getValue("ydest") != null) _move.setYdest(Double.parseDouble(att.getValue("ydest")));          
            if(att.getValue("maxSpeed") != null) _move.setSpeed(Double.parseDouble(att.getValue("maxSpeed")));
            if(att.getValue("duration") != null) _move.setTime(Double.parseDouble(att.getValue("duration")));
            if( (att.getValue("rSpeed")!=null) && (att.getValue("rSpeed").equals("true")) ) _move.setRSpeed(true);
            _traj.add(_pause);
        }
	
	if (qName.equals("secondlayerprotocol")){
            _kernel.getSimulator().setCommTui(_mode);
  	    if(att.getValue("name").equals("CSMA_CA")) { _kernel.getSimulator().setProtocol2("CsmaCa"); _slp=_kernel.getSimulator().getProtocole(); }
	    if(att.getValue("name").equals("RENDEZVOUS")) { _kernel.getSimulator().setProtocol2("Rendez-Vous"); _slp=_kernel.getSimulator().getProtocole(); }

	}
	if (qName.equals("thirdlayerprotocol")){
  	    // TODO : what to do with this ?
	    // _kernel.getSimulator().setProtocol3(att.getValue("name"));
	    // if(_mode==1) { _kernel.getSimulator().setCommunicationManager(new CommunicationManager(_kernel.getSimulator(), new ProtocoleCsmaca(_kernel.getSimulator()),new AODVProtocol(_kernel.getSimulator()))); }
	    // else
	    //{ _kernel.getSimulator().setCommunicationManager(new CommunicationManager(_kernel.getSimulator(), new ProtocoleCsmaca(_kernel.getSimulator()),_mode)); }
	}
	if(qName.equals("obstacle")){
	    _inObstacle=true;
            _obstacle=new DataObstacle();
            // TODO: the name!
            _obstacle.setName(att.getValue("name"));
            
            _obstacle.setAlpha(Double.parseDouble(att.getValue("brakingRate")));
            _obstacle.setGamma(Double.parseDouble(att.getValue("waveBrakingRate")));
            
	}
	if(qName.equals("circle")){
	    _shape = new Circle(Double.parseDouble(att.getValue("x")), Double.parseDouble(att.getValue("y")), Double.parseDouble(att.getValue("diameter")));
	    if(_inObstacle)
	    {
	      _objObst=new Obstacle(_shape);
	      _objObst.setProperties(_obstacle);
	    }
	}
	if(qName.equals("rectangle")){
	    _shape = new Rectangle(Double.parseDouble(att.getValue("x")), Double.parseDouble(att.getValue("y")), Double.parseDouble(att.getValue("width")), Double.parseDouble(att.getValue("height")));
	    if(_inObstacle)
	    {
	      _objObst=new Obstacle(_shape);
	      _objObst.setProperties(_obstacle);
	    }
	    if(_inSensorNetwork)
	    {
	        _sensorNetwork.setCoordX(Double.parseDouble(att.getValue("x")));
	        _sensorNetwork.setCoordY(Double.parseDouble(att.getValue("y")));
	        _sensorNetwork.setWidth(Double.parseDouble(att.getValue("width")));
	        _sensorNetwork.setHeight(Double.parseDouble(att.getValue("height")));
	    }
        }
	if(qName.equals("environment")){
	    _kernel.getSimulator().setEnvironment(Double.parseDouble(att.getValue("width")),
                                                  Double.parseDouble(att.getValue("height")));
	}
	if(qName.equals("simulation")){
            if(_mode!=3)
            {
	        _dataSimulator = new DataSimulator(0);
	        _dataSimulator.setResultDir(_outputDir);
	        _dataSimulator.setName(att.getValue("name"));
	        _dataSimulator.setDescription(att.getValue("description"));
	        _dataSimulator.setDuration(Double.parseDouble(att.getValue("duration")));
	        String s=att.getValue("type");
  	        if(s.equals("infrastructure")) { _dataSimulator.setMode(0); _mode=0; }
                if(s.equals("ad_hoc")) { _dataSimulator.setMode(1); _mode=1; }
                if(s.equals("hybrid")) { _dataSimulator.setMode(2); _mode=2;}
            }
            if(_mode==3)
            {
	        _sensorSimulator = new DataSensorSimulator(3);
	        _sensorSimulator.setResultDir(_outputDir);
	        _sensorSimulator.setName(att.getValue("name"));
	        _sensorSimulator.setDescription(att.getValue("description"));
	        _sensorSimulator.setDuration(Double.parseDouble(att.getValue("duration")));
	        String s=att.getValue("type");
                _sensorSimulator.setMode(3); _mode=3;
            }            
	}
	if(qName.equals("objects")){
	}
	if((qName.equals("probe")) && (_probeType.equals("base")) ){
            _typeBaseProbe = new String(att.getValue("type"));
            if(_typeBaseProbe.equals("results.ProbeComNbInterferences")){
                Vector vp=_baseStation.getProbes();
                vp.add(new String("ProbeComNbInterferences"));
                _baseStation.setProbes(vp);
            }
            if(_typeBaseProbe.equals("results.ProbeComState")){
                Vector vp=_baseStation.getProbes();
                vp.add(new String("ProbeComState"));
                _baseStation.setProbes(vp);
            }
            if(_typeBaseProbe.equals("results.ProbeComReachRate")){
                Vector vp=_baseStation.getProbes();
                vp.add(new String("ProbeComReachRate"));
                _baseStation.setProbes(vp);
            }
            if(_typeBaseProbe.equals("results.ProbeComReachLength")){
                Vector vp=_baseStation.getProbes();
                vp.add(new String("ProbeComReachLength"));
                _baseStation.setProbes(vp);
            }
            if(_typeBaseProbe.equals("results.ProbeComCarrierLength")){
                Vector vp=_baseStation.getProbes();
                vp.add(new String("ProbeComCarrierLength"));
                _baseStation.setProbes(vp);
            }
            if(_typeBaseProbe.equals("results.ProbeComReachableNeighbors")){
                Vector vp=_baseStation.getProbes();
                vp.add(new String("ProbeComReachableNeighbors"));
                _baseStation.setProbes(vp);
            }
            if(_typeBaseProbe.equals("results.ProbeComCarrierNeighbors")){
                Vector vp=_baseStation.getProbes();
                vp.add(new String("ProbeComCarrierNeighbors"));
                _baseStation.setProbes(vp);
            }
/*            if(_typeBaseProbe.equals("results.ProbeComWishedEmissions")){
                ProbeComWishedEmissions p = new ProbeComWishedEmissions(_objBaseStation,"ProbeComWishedEmissions");
                _kernel.getSimulator().getResultManager().addProbe(p);
            }*/
/*            if(_typeBaseProbe.equals("results.ProbeComCurrentCommunication")){
                ProbeComCurrentCommunication p = new ProbeComCurrentCommunication(_objBaseStation,"ProbeComCurrentCommunication");
                _kernel.getSimulator().getResultManager().addProbe(p);
            }*/
        }
	if( (qName.equals("probe")) && (_probeType.equals("mobile")) ){
  	    _typeMobileProbe = new String(att.getValue("type"));
            if(_typeMobileProbe.equals("results.ProbeComNbInterferences")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeComNbInterferences"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeComState")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeComState"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeComReachRate")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeComReachRate"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeComReachLength")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeComReachLength"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeComCarrierLength")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeComCarrierLength"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeComReachableNeighbors")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeComReachableNeighbors"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeComCarrierNeighbors")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeComCarrierNeighbors"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeMovePosition")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeMovePosition"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeEnergyBatteryLevel")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeEnergyBatteryLevel"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeMoveSpeed")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeMoveSpeed"));
                _mobileStation.setProbes(vp);
            }
            if(_typeMobileProbe.equals("results.ProbeEnergyConsumption")){
                Vector vp=_mobileStation.getProbes();
                vp.add(new String("ProbeEnergyConsumption"));
                _mobileStation.setProbes(vp);
            }
	}
	if((qName.equals("probe")) && (_probeType.equals("sink")) ){
	    _typeSinkProbe = att.getValue("type");
            if(_typeSinkProbe.equals("results.ProbeComNbInterferences")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeComNbInterferences"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeComState")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeComState"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeComReachRate")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeComReachRate"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeComReachLength")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeComReachLength"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeComCarrierLength")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeComCarrierLength"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeComReachableNeighbors")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeComReachableNeighbors"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeComCarrierNeighbors")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeComCarrierNeighbors"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeMovePosition")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeMovePosition"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeEnergyBatteryLevel")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeEnergyBatteryLevel"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeMoveSpeed")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeMoveSpeed"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeEnergyConsumption")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeEnergyConsumption"));
                _sink.setProbes(vp);
            }
            if(_typeSinkProbe.equals("results.ProbeDirectedDiffusion")){
                Vector vp=_sink.getProbes();
                vp.add(new String("ProbeDirectedDiffusion"));
                _sink.setProbes(vp);
            }
	}
	if((qName.equals("probe")) && (_probeType.equals("sensornetwork")) ){
	    _typeSensorNetworkProbe = att.getValue("type");
            if(_typeSensorNetworkProbe.equals("results.ProbeKCover")){
                Vector vp=_sensorNetwork.getProbes();
                vp.add(new String("ProbeKCover"));
                _sensorNetwork.setProbes(vp);
            }
            if(_typeSensorNetworkProbe.equals("results.ProbeDeadSensor")){
                Vector vp=_sensorNetwork.getProbes();
                vp.add(new String("ProbeDeadSensor"));
                _sensorNetwork.setProbes(vp);
            }
            if(_typeSensorNetworkProbe.equals("results.ProbeSleepingSensor")){
                Vector vp=_sensorNetwork.getProbes();
                vp.add(new String("ProbeSleepingSensor"));
                _sensorNetwork.setProbes(vp);
            }
            if(_typeSensorNetworkProbe.equals("results.ProbeGlobalEnergy")){
                Vector vp=_sensorNetwork.getProbes();
                vp.add(new String("ProbeGlobalEnergy"));
                _sensorNetwork.setProbes(vp);
            }
        }
	_prevqName=new String(qName);
    }

    public void characters(char[] ch, int start, int length) 
	throws SAXException
    {
    }

    public void endElement(String uri, String localName, String qName)
	throws SAXException
    {
	Element e = _elements.pop();
	//	Debug.getConfigLogger().info(e.toString());
	if(qName.equals("simmobil")){

            if(_mode==3)
            {
/*                System.out.println("taille:"+((SensorSimulator)(_kernel.getSimulator())).getSinkVector().size());
                ((SensorSimulator)(_kernel.getSimulator())).setProperties(_sensorSimulator);
                for(int i = 0; i < ((SensorSimulator)(_kernel.getSimulator())).getSinkVector().size(); i++)
                {
                    Sink b = (Sink) (((SensorSimulator)(_kernel.getSimulator())).getSinkVector().get(i));
                    DataSink data = new DataSink();
                    data=(DataSink)b.getProperties();
                    b.setProperties(data);
                }
                for(int i = 0; i < ((SensorSimulator)(_kernel.getSimulator())).getSensorNetworkVector().size(); i++)
                {
                    SensorNetwork b= (SensorNetwork)((SensorSimulator)(_kernel.getSimulator())).getSensorNetworkVector().get(i);
                    DataSensorNetwork data = new DataSensorNetwork();
                    data=(DataSensorNetwork)b.getProperties();
                    for(int j = 0; j < b.getSensorVect().size(); j++)
                    {
                        Sensor mySensor = (b.getSensorVect()).get(j);
                        DataSensor data2 = new DataSensor();
                        data2=(DataSensor)mySensor.getProperties();
                        mySensor.setProperties(data2);
                    }
                    b.setProperties(data);
                }*/
                ((SensorSimulator)(_kernel.getSimulator())).setProperties(_sensorSimulator);
            }
             else
            {
/*                for(int i = 0; i < _kernel.getSimulator().getBaseStation().size(); i++)
                {
                    BaseStation b= (BaseStation) (_kernel.getSimulator().getBaseStation()).get(i);
                    DataBaseStation data = new DataBaseStation();
                    data=(DataBaseStation)b.getProperties();
                    b.setProperties(data);
                }
                for(int i=0;i<_kernel.getSimulator().getMobileStation().size(); i++)
                {
                    MobileStation b= (MobileStation) (_kernel.getSimulator().getMobileStation()).get(i);
                    DataMobileStation data = new DataMobileStation();
                    data=(DataMobileStation)b.getProperties();
                    b.setProperties(data);
                }*/                
                _kernel.getSimulator().setProperties(_dataSimulator);
            }
	}
	if(qName.equals("mobile")){
            if(!(_traj.isEmpty())) _mobileStation.setTrajectory(_traj);
	    _objMobileStation.setProperties(_mobileStation);
	    _kernel.getSimulator().addMobileStation(_objMobileStation);
	}
	if(qName.equals("sink")){
//            if(!(_traj.isEmpty())) _sink.setTrajectory(_traj);
            _objSink.setProperties(_sink);
 	    ((SensorSimulator)(_kernel.getSimulator())).addSink(_objSink);
	}

	if(qName.equals("sensor")){

            _objSensor.setProperties(_sensor);	
	    if(_inSensorNetwork)
	    {
	      _sensorNetwork.setDataSensor(_sensor);
	    }
	     else
	    {
            }
	}

	if(qName.equals("sensornetwork")){

	    _objSensorNetwork.setProperties(_sensorNetwork);
	    ((SensorSimulator)(_kernel.getSimulator())).addSensorNetwork(_objSensorNetwork);
	    _inSensorNetwork=false;
	}

	if(qName.equals("base")){

	  _objBaseStation.setProperties(_baseStation);
	  _kernel.getSimulator().addBaseStation(_objBaseStation);
	}

	if(qName.equals("communication")){
	    if(_mode!=1)
	    {
  	        // adds the communication to the related station
	        if(_currentTypeStation.equals("base")){
		    Vector emissions = _baseStation.getWishedEmission();
		    emissions.add(_emission);
		    _baseStation.setWishedEmission(emissions);
	        }
	        if(_currentTypeStation.equals("mobile")){
		    Vector emissions = _mobileStation.getWishedEmission();
		    emissions.add(_emission);
		    _mobileStation.setWishedEmission(emissions);
	        }
	    }
	    if(_mode==1)
	    {
  	        // adds the communication to the related station
	        if(_currentTypeStation.equals("mobile")){
		    Vector emissions = _mobileStation.getWishedEmission3();
		    emissions.add(_emission3);
		    _mobileStation.setWishedEmission3(emissions);
	        }
	    }	    
	}

	if(qName.equals("destination")){
	}

	// Trajectories
        if(qName.equals("trajectory")) {
        }

	if(qName.equals("pause")){
	}

	if(qName.equals("move")){
        }
	
	if (qName.equals("secondlayerprotocol")){
	}

	if (qName.equals("thirdlayerprotocol")){
        }

	if(qName.equals("obstacle")){
	    _inObstacle=false;
	    try{
	        _kernel.getSimulator().addObstacle(_objObst);
            }
	    catch(OutOfSpaceException ex){
                Debug.getGUILogger().warning("Obstacle not added : location defined out of space");
	    }
	}

	if(qName.equals("circle")){
	}

	if(qName.equals("rectangle")){
        }

	if(qName.equals("environment")){
	}

	if(qName.equals("simulation")){
	}

	if(qName.equals("objects")){
	}

	if(qName.equals("generalprobe")){
	}

	if(qName.equals("baseprobe")){
	}

	if(qName.equals("mobileprobe")){
	}

	if(qName.equals("sinkprobe")){
	}

	if(qName.equals("sensornetworkprobe")){
	}	
    }

    public void endDocument()
	throws SAXException
    {
	//	Debug.getConfigLogger().info("document end \n");

	DataType _object;
	for(int i =0;i<_objects.size();i++){
	    _object = _objects.get(i);
	    //   Debug.getConfigLogger().info(_object.toString());
	}
    }

    public void error(SAXParseException e) throws SAXParseException
    {
	throw e;
    }
}
 


class Element
{
    String _name;
    Hashtable<String,String> _args;

    public Element(String name, Attributes a){
	_name = name;
	_args = new Hashtable<String,String>();
	for(int i = 0; i < a.getLength(); i++){
	    _args.put(a.getQName(i),a.getValue(i));
	}
    }
    public String toString(){
	return (_name + " : " + _args);
    }
    public String getName(){
	return _name;
    }
    public Hashtable getAttr(){
	return _args;
    }
}
